$(document).ready(function () {
 		var arr = area();
		for(var i=0; i<arr.length;i++){
			$("#change").append("<option value="+arr[i].areaCode+">"+arr[i].province+" "+arr[i].city+" "+arr[i].area+"</option>");
		}			
			
});

//添加配送费
function add() {
	//获得地区信息
	var content = $("#change").find("option:selected").text();
	var arr = content.split(' ');
	var provinceName = arr[0];
	var cityName = arr[1];
	var areaName = arr[2];
	var areaCode = $("#change").val();
	
	var areaLength1 = "0.99";
	var deliverTime1 = $('#deliverTime1').val();
	var areaLength2 = "1.99";
	var deliverTime2 = $('#deliverTime2').val();
	var areaLength3 = "2.99";
	var deliverTime3 = $('#deliverTime3').val();
	var areaLength4 = "3.99";
	var deliverTime4 = $('#deliverTime4').val();
	var areaLength5 = "4.99";
	var deliverTime5 = $('#deliverTime5').val();
	var areaLength6 = "5";
	var deliverTime6 = $('#deliverTime6').val();	
	var sendFee1 = $('#sendFee1').val();
	var sendFee2 = $('#sendFee2').val();
	var sendFee3= $('#sendFee3').val();
	var sendFee4 = $('#sendFee4').val();
	var sendFee5 = $('#sendFee5').val();
	var sendFee6 = $('#sendFee6').val();
	if (deliverTime1=='' || deliverTime2=='' || deliverTime3=='' || deliverTime4=='' || deliverTime5=='' || deliverTime6=='') {
      alert('配送时间不能为空');
    return;
    }
	if (sendFee1=='' || sendFee2=='' || sendFee3=='' || sendFee4=='' || sendFee5=='' || sendFee6=='') {
      alert('配送费不能为空');
    return;
    }
    $.ajax({
        url:  urlcore + "/api/sendRates/addOrUpdateSendRates",
       		type: "post",
       		data:JSON.stringify([
       			{
	            "areaCode": areaCode,
	            "areaLength": areaLength1,
	            "deliverTime": deliverTime1,
	            "sendFee": sendFee1,
	            "province": provinceName,
	            "city": cityName,
	            "area": areaName},
	            {
	            "areaCode": areaCode,
	            "areaLength": areaLength2,
	            "deliverTime": deliverTime2,
	            "sendFee": sendFee2,
	            "province": provinceName,
	            "city": cityName,
	            "area": areaName},
	            {
	            "areaCode": areaCode,
	            "areaLength": areaLength3,
	            "deliverTime": deliverTime3,
	            "sendFee": sendFee3,
	            "province": provinceName,
	            "city": cityName,
	            "area": areaName},
	            {
	            "areaCode": areaCode,
	            "areaLength": areaLength4,
	            "deliverTime": deliverTime4,
	            "sendFee": sendFee4,
	            "province": provinceName,
	            "city": cityName,
	            "area": areaName},
	            {
	            "areaCode": areaCode,
	            "areaLength": areaLength5,
	            "deliverTime": deliverTime5,
	            "sendFee": sendFee5,
	            "province": provinceName,
	            "city": cityName,
	            "area": areaName},
	           	{
	            "areaCode": areaCode,
	            "areaLength": areaLength6,
	            "deliverTime": deliverTime6,
	            "sendFee": sendFee6,
	            "province": provinceName,
	            "city": cityName,
	            "area": areaName}
   		    ]),
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
				if (data.success == true) {             
                    if (confirm("添加成功，是否继续添加？")) {
                        //刷新
                        location.reload();
                    } else {
                        //跳转列表页
                        window.location.href = "setting_list.html";
                    }	                   
                }
             else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }

        },
        error: function() {
            alert("error");
        }

    });
}

//获得地区
function area(){
	var da='';
	$.ajax({
            url: urlcore + "/api/openArea/allArea",
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            success:function(data){
                if (data.success == true) {
                    da = data.data;                   
	                /* $('#messUrl').html(imgStr2);                  
                    $('#title').text(da.title);
                    $('#content').html(da.content);*/
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
        return da;
}
